package com.lw.extraceim.app;

import android.app.Activity;
import android.app.Application;
import android.app.ProgressDialog;
import android.util.Log;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.lw.extraceim.model.UserInfo;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.cookie.CookieJarImpl;
import com.zhy.http.okhttp.cookie.store.PersistentCookieStore;
import com.zhy.http.okhttp.https.HttpsUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.OkHttpClient;


public class ExtraceApplication extends Application {
    private static final String TAG = "ExtraceApplication";
    private List<Activity> activityList; //存放正在运行的activity
    private UserInfo userInfo; //当前登录的用户信息
    private ExtraceCfg extraceCfg; //app的配置类
    private int scanActivityRequestCode;//条形码界面的请求码
    private String sessionId;//登录过后的sessionId


    @Override
    public void onCreate() {
        super.onCreate();
        activityList = new LinkedList<>();
        //配置cookie
        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
        //配置https
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                .addInterceptor(new LoggerInterceptor("TAG"))
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                })
                .cookieJar(cookieJar)
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .build();
        OkHttpUtils.initClient(okHttpClient); //设置默认OkHttpClient
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        SDKInitializer.initialize(this);
        //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);
    }

    public void onTerminate() {
        super.onTerminate();

        //save data of the map
    }


    public ExtraceCfg getExtraceCfg() {
        extraceCfg = ExtraceCfg.getExtraceCfg(this);
        return extraceCfg;
    }

    /**
     * 保存配置文件
     */
    public void saveExtraceCfg() {
        if (extraceCfg != null) {
            extraceCfg.save();
        }
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    /**
     * 添加Activity
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (!activityList.contains(activity)) {
            activityList.add(activity);
        }
    }

    /**
     * 移出activity
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        if (activityList.contains(activity)) {
            activityList.remove(activity);
        }
    }

    public int getScanActivityRequestCode() {
        return scanActivityRequestCode;
    }

    public void setScanActivityRequestCode(int scanActivityRequestCode) {
        this.scanActivityRequestCode = scanActivityRequestCode;
    }

    /**
     * 退出app，并销毁所有Activity
     */
    public void exitApp() {
        Log.d(TAG, "容器中的Activity列表如下：");
        for (Activity activity : activityList) {
            Log.d(TAG, activity.getLocalClassName());
        }
        Log.d(TAG, "逐步退出容器内所有Activity");
        for (Activity activity : activityList) {
            activity.finish();
        }
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

}
